package com.zhb.services;

/**
 * Created by jarvan4dev on 16/5/22.
 */

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.javadoc.Doc;
import com.zhb.entity.BookInfo;
import com.zhb.entity.Loan;
import com.zhb.util.HtmlUtils;
import com.zhb.util.HttpUtils;
import org.apache.http.client.HttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 借阅
 */
public class LoanService {

    private final HttpClient httpClient;

    private static final String loanUri = "http://27.191.198.3:7088/opac/loan/currentLoanList";

    private static final String renewalUri = "http://27.191.198.3:7088/opac/loan/doRenew";
    public LoanService(HttpClient httpClient){
        this.httpClient = httpClient;
    }

    // 访问借阅查询页面
    public String visitLoanPage() throws IOException {
        return HttpUtils.doGet(httpClient, loanUri);
    }

    public List<Loan> getMyLoan(String htmlSource) throws IOException {
        Document document = Jsoup.parse(HtmlUtils.formatHtml(htmlSource));
        Element contentTable = document.getElementById("contentTable");
        Elements trEles = contentTable.getElementsByTag("tr");
        List<List<Object>> infoList = Lists.newArrayList();
        for (int i = 1; i < trEles.size(); i++){
            List<Object> info = Lists.newArrayList();
            Element trEle = trEles.get(i);
            Elements tds = trEle.children();
            String bookInfoUri = "http://27.191.198.3:7088" + tds.get(1).child(0).attr("href");
            String html = visitBookInfoPage(bookInfoUri);
            BookInfo bookInfo = getBookInfo(html);
            for (Element td : tds){
                info.add(td.text());
            }
            info.add(bookInfo);
            infoList.add(info);
        }
        return assembleLoanInfo(infoList);
    }

    private BookInfo getBookInfo(String html) {
        BookInfo bookInfo = new BookInfo();
        Document document = Jsoup.parse(html);
        Element bookCoverImg = document.getElementById("bookcover_img");
        bookInfo.setCoverLink(bookCoverImg.attr("src"));
        Element bookInfoTable = document.getElementById("bookInfoTable");
        Elements infoTds = bookInfoTable.getElementsByClass("rightTD");
        assemblyBookInfo(infoTds, bookInfo);
        return bookInfo;
    }

    private void assemblyBookInfo(Elements infoTds, BookInfo bookInfo){
        String info = infoTds.get(0).text();
        bookInfo.setISBN(info.split(" ")[0]);
        bookInfo.setPrice(info.split(" ")[2]);
        bookInfo.setLang(infoTds.get(1).text());
        bookInfo.setBookName(infoTds.get(2).child(0).text());
        bookInfo.setPublisher(infoTds.get(3).child(0).text());
        bookInfo.setPublishDate(infoTds.get(3).text().split("出版日期：")[1]);
        bookInfo.setStandard(infoTds.get(4).text());
        bookInfo.setMark(infoTds.get(5).text());
        bookInfo.setBriefIntro(infoTds.get(6).text());
        bookInfo.setTheme(infoTds.get(7).text());
        bookInfo.setCLC(infoTds.get(8).text().split(" ")[0]);
    }

    private String visitBookInfoPage(String bookInfoUri) throws IOException {
        return HttpUtils.doGet(httpClient, bookInfoUri);
    }

    // 续借的条码号
    public void doRenewal(String qrCode) throws IOException {
        Map<String, Object> params = Maps.newHashMap();
        params.put("", qrCode);
        params.put("furl", "/opac/loan/renewList");
        params.put("renewAll", "");
        String html = HttpUtils.doPost(httpClient, renewalUri, params);
        Document document = Jsoup.parse(html);
        Element element = document.getElementById("content");
        System.out.println(element.select("div").first().text());
    }

    private List<Loan> assembleLoanInfo(List<List<Object>> infoList){
        List<Loan> loanList = Lists.newArrayList();
        for (List<Object> info : infoList){
            Loan loan = new Loan();
            loan.setQrCode(String.valueOf(info.get(0)));
            loan.setStoreLib(String.valueOf(info.get(4)));
            loan.setDocType(String.valueOf(info.get(5)));
            loan.setBorrowDate(String.valueOf(info.get(6)));
            loan.setReturnDate(String.valueOf(info.get(7)));
            loan.setBookInfo((BookInfo) info.get(8));
            loanList.add(loan);
        }
        return loanList;
    }
}
